﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
using MongoDB.Driver.Linq;
using MongoDB.Driver;
using MongoDB.Bson;
namespace YmtUserAuth.DataImport
{
    public class MongoRepository<TEntity>
    {
        public static void Insert(IEnumerable<TEntity> entity)
        {

            var collectionName = Configure.GetCollectionName(typeof(TEntity).FullName);
            var result = MongoDbDatabaseServer.BuildServer.GetTypeCollection<TEntity>(collectionName).InsertBatch(entity);

        }

        public static void Insert(string tb, string db, IEnumerable<TEntity> val)
        {
            if (val == null || val.Count() == 0) return;
            if (val.Count() == 1)
                MongoDbDatabaseServer.BuildServer.GetTypeCollection<TEntity>(db, tb).Insert(val.ToArray()[0]);
            else
                MongoDbDatabaseServer.BuildServer.GetTypeCollection<TEntity>(db, tb).InsertBatch(val);
        }

        public static IEnumerable<TEntity> Find(Func<TEntity, bool> exp)
        {
            var collectionName = Configure.GetCollectionName(typeof(TEntity).FullName);
            return MongoDbDatabaseServer.BuildServer.GetTypeCollection<TEntity>(collectionName).AsQueryable().Where(exp);
        }

        public static IEnumerable<TEntity> Find(IMongoQuery query)
        {
            var collectionName = Configure.GetCollectionName(typeof(TEntity).FullName);
            return MongoDbDatabaseServer.BuildServer.GetTypeCollection<TEntity>(collectionName).Find(query);
        }
    }
}
